package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.*;
import org.jfree.chart.*;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import Entity.Report;

import com.lowagie.text.pdf.events.IndexEvents.Entry;

public class ViewReport {

	private JFrame bigFrame = new JFrame();
	int i=0;
	public ViewReport(){}
	
	
	public void eInPeriod(HashMap<String,Integer[]> arr) {
		
		for (java.util.Map.Entry<String, Integer[]> e : arr.entrySet()) {
		  
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			Integer[] ar = e.getValue();
			
			if(ar[0]!=0)
				dataset.setValue(ar[0], "Events", "January");
			if(ar[1]!=0)
				dataset.setValue(ar[1], "Events", "February");
			if(ar[2]!=0)
				dataset.setValue(ar[2], "Events", "March");
			if(ar[3]!=0)
				dataset.setValue(ar[3], "Events", "April");
			if(ar[4]!=0)
				dataset.setValue(ar[4], "Events", "May");
			if(ar[5]!=0)
				dataset.setValue(ar[5], "Events", "June");
			if(ar[6]!=0)
				dataset.setValue(ar[6], "Events", "July");
			if(ar[7]!=0)
				dataset.setValue(ar[7], "Events", "August");
			if(ar[8]!=0)
				dataset.setValue(ar[8], "Events", "September");
			if(ar[9]!=0)
				dataset.setValue(ar[9], "Events", "October");
			if(ar[10]!=0)
				dataset.setValue(ar[10], "Events", "November");
			if(ar[11]!=0)
				dataset.setValue(ar[11], "Events", "December");
			
			JFreeChart chart = ChartFactory.createBarChart3D("Year - "+e.getKey()+"", "Month", "Events", dataset, PlotOrientation.VERTICAL, false, true, false);
			chart.getTitle().setPaint(Color.blue);
			final CategoryPlot p = chart.getCategoryPlot();
			BarRenderer renderer = (BarRenderer)p.getRenderer();
			DecimalFormat decimalformat = new DecimalFormat("##.##");
			renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalformat));
			p.setRenderer(renderer);
			renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
			renderer.setItemLabelsVisible(true);
			chart.getCategoryPlot().setRenderer(renderer);
			
			p.setRangeGridlinePaint(Color.black);
			ChartFrame panel = new ChartFrame("Events In Period", chart);
			panel.setVisible(true);
			panel.setSize(450, 350);
			try {
				final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
				final File file1 = new File("Chart.png");
				ChartUtilities.saveChartAsPNG(file1, chart, 600, 400,info);
			} catch (Exception e2) {
				System.out.println("Couldn't save the chart");
			}
			Container contentPane = bigFrame.getContentPane();
			contentPane.setLayout(new FlowLayout());			
			bigFrame.add(panel);
			bigFrame.pack();
			
		}
		bigFrame.setVisible(true);
	}
	
	public void holePerPetiod (HashMap<String, ArrayList<Report>> HM){
		//ChartFrame bigFrame = new ChartFrame("Halls In Period", chart);
		//JPanel p = new JPanel();
		for (java.util.Map.Entry<String, ArrayList<Report>> e : HM.entrySet()) {
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			ArrayList<Report> list =new ArrayList<>();
			list = e.getValue();
			for(Report r: list){
				dataset.setValue(r.getObjectCnt(), "Halls", r.getObject());
			}	
			JFreeChart chart = ChartFactory.createBarChart3D("Years - "+e.getKey()+"", "Halls", "Events", dataset, PlotOrientation.VERTICAL, false, true, false);
			chart.getTitle().setPaint(Color.blue);
			final CategoryPlot p = chart.getCategoryPlot();
			BarRenderer renderer = (BarRenderer)p.getRenderer();
			DecimalFormat decimalformat = new DecimalFormat("##.##");
			renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",decimalformat));
			p.setRenderer(renderer);
			renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
			renderer.setItemLabelsVisible(true);
			chart.getCategoryPlot().setRenderer(renderer);
				
			p.setRangeGridlinePaint(Color.black);
			ChartPanel panel = new ChartPanel(chart);
			panel.setVisible(true);
			panel.setSize(450, 350);
			try {
				final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
				final File file1 = new File("Chart.png");
				ChartUtilities.saveChartAsPNG(file1, chart, 600, 400,info);
			} catch (Exception e2) {
				System.out.println("Couldn't save the chart");
			}
			
			Container contentPane = bigFrame.getContentPane();
			contentPane.setLayout(new FlowLayout());			
			bigFrame.add(panel );
			bigFrame.pack();
			
		}
		bigFrame.setVisible(true);
	}

	public void MeetingPerPetiod(){
		
	}
}